<?php

    class Sms extends CApplicationComponent
    {
        public function init(){
            parent::init();
        }
        
        /*
        * link : http://smsvl.tk/restful/insert_sms
        * Method : Post
        * param input : phone_number = so dien thoai / danh sach sdt ( 0976451089;01682289699;0944886488)
        *               sms = nội dung sms ( tiếng anh / tiếng việt )
        *               username = anuong.net
        *               password = MD5(pass)
        * return : 
        - Neu xay ra loi : return : phone error - can not insert db; // danh sach cac so bi loi
        - Neu thanh cong : return : null
        */
        public function send($phone, $msg, $bill_id = null, $manager_id = null){
            $smsNoticeData = SmsNotice::model()->getLastNotice(); 
            if(!$smsNoticeData['status']) return;
            
            $acc = array(
                'mh' => array(
                    'username' => 'Manhhai',
                    'password' => 'd0970714757783e6cf17b26fb8e2298f'/*112233*/,
                ),
                'au' => array(
                    'username' => 'anuong.net',
                    'password' => 'd1749ec0584855a372f844fc068cc687'/*amthuc@789*/
                ),
            );
            
//            $username = IS_LOCAL ? $acc['au']['username'] : $acc['mh']['username'];
//            $password = IS_LOCAL ? $acc['au']['password'] : $acc['mh']['password'];
            
            $username = $acc['mh']['username'];
            $password = $acc['mh']['password'];
            
            //            $client=new SoapClient('http://smsvl.tk/sms/index.php?wsdl');
            //            $client->insert_data($phone, $msg, 0, $username, $password, 'en'); 
            Yii::import('ext.Curl');
            $curl = new Curl('http://smsvl.tk/restful/insert_sms');
            $curl->posts = array(
                'phone_number'     => $phone,
                'sms'   => $msg,
                'username'   => $username,
                'password'   => $password,
            );
            $curl->run();
            
            $smsLog = new SmsLog;
            $smsLog->phone = $phone;
            $smsLog->message = $msg;
            $smsLog->bill_id = $bill_id;
            $smsLog->manager_id = $manager_id;
            $smsLog->created = MyDateTime::getCurrentTime();
            $smsLog->save();
        }
            

}